﻿using M.Warehouse.API.Application.Command.InventoryCommand;
using M.Warehouse.Domain;
using M.Warehouse.Repository.WarehouseInterface;
using MediatR;

namespace M.Warehouse.API.Application.CommandHandlers.InventoryHandlers
{
    /// <summary>
    /// 入库显示
    /// </summary>
    public class InventoryShowCommandHandlers : IRequestHandler<InventoryShowCommand, List<Inventory>>
    {
        private readonly IInventoryRepository _inventoryRepository;
        public InventoryShowCommandHandlers(IInventoryRepository inventoryRepository)
        {
            _inventoryRepository = inventoryRepository;
        }
        public async Task<List<Inventory>> Handle(InventoryShowCommand request, CancellationToken cancellationToken)
        {
            var list = (await _inventoryRepository.GetAllAsync()).Where(x => !x.IsDeleted&&x.InventoryType==1);
            if (!string.IsNullOrEmpty(request.InventoryNo))
            {
                list = list.Where(x => x.InventoryNo.Contains(request.InventoryNo));
            }
            if(!string.IsNullOrEmpty(request.FinishedProductNo))
            {
                list = list.Where(x => x.FinishedProductNo.Contains(request.FinishedProductNo));
            }
            if(request.InventoryState!=null)
            {
                list = list.Where(x => x.InventoryState == request.InventoryState);
            }
            list=list.OrderByDescending(x=>x.CreatedDate);
            return list.ToList();
        }
    }
}
